package com.vogue.supplymall.order.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.vogue.supplymall.common.domain.dto.CommentDefaultAnswerInfo;
import com.vogue.supplymall.common.domain.dto.CommentInfo;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.vogue.supplymall.order.domain.service.CommentDefaultAnswerService;
import com.vogue.supplymall.order.domain.service.CommentService;

/**
 * 买家卖家评价
 */
@RestController
public class CommentController extends BaseController {


    public static final Logger LOGGER_COMMENTCONTROLLER = LoggerFactory.getLogger(CommentController.class);

    @Resource
    private CommentService commentService;
    @Resource
    private MessageSourceUtils messageSourceUtils;
    @Resource
   private CommentDefaultAnswerService commentDefaultAnswerService;

    @RequestMapping(value="/is/{appver}/order/saveComment",method=RequestMethod.POST)
	public ResponseEntity<Object> saveComment(@PathVariable("appver") String appver,@RequestBody CommentInfo commentInfo) {
        LOGGER_COMMENTCONTROLLER.info("add order start >>>>>> ");
	        UserInfo info = new UserInfo();
	        if (SecurityUtils.getSubject().isAuthenticated()) {
	            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
                LOGGER_COMMENTCONTROLLER.info("获取登信息：" + info.getLoginId());
	        } else {
                LOGGER_COMMENTCONTROLLER.info("获取登信息失败！");
	        }
	        
	        commentService.saveOrUpdateCommentInfo(commentInfo, info);
	        
	        ControllerResultInfo resultInfo = new ControllerResultInfo();
	        resultInfo.setResult(true);
	        resultInfo.setData(null);
	        
	       return new ResponseEntity<>(resultInfo, HttpStatus.OK);
	}


    /**
     * 获取待评价数量（买家或卖家）
     * @param appver
     * @return
     */
    @RequestMapping(value = "/is/{appver}/order/getCommentingCount", method = RequestMethod.POST)
    public ResponseEntity<Object> getCommentingCount(@PathVariable("appver") String appver) {
        LOGGER_COMMENTCONTROLLER.info(">>>>>> getCommentingCount start");
        CommentInfo commentInfo = new CommentInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            commentInfo.setLoginId(info.getLoginId());
            if (null != info.getOrganInfo()) {
                commentInfo.setOrganid(info.getOrganInfo().getId());
            } else {
                commentInfo.setOrganid(info.getLoginId());
            }
            commentInfo.setSyscodeid(info.getSyscodeid());
        } else {
            LOGGER_COMMENTCONTROLLER.info("未登录");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(commentService.getCommentingCount(commentInfo));
        LOGGER_COMMENTCONTROLLER.info(" <<<<<< getCommentingCount end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 买家评论功能--评论列表
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/buyer/commentList", method = RequestMethod.POST)
    public ResponseEntity<Object> getCommentList(@PathVariable("appver") String appver, @RequestBody(required = false) CommentInfo commentInfo) {
        LOGGER_COMMENTCONTROLLER.info("get buyer comment list ");
        if (commentInfo == null) {
            commentInfo = new CommentInfo();
        }
        UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        commentInfo.setLoginId(info.getLoginId());
        if (null != info.getOrganInfo()) {
            commentInfo.setOrganid(info.getOrganInfo().getId());
        } else {
            commentInfo.setOrganid(info.getLoginId());
        }
        commentInfo.setSyscodeid(info.getSyscodeid());


        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(commentService.findBuyerCommentList(commentInfo));
        LOGGER_COMMENTCONTROLLER.info("get buyer comment list end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 卖家评论功能--评论列表
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/seller/commentList", method = RequestMethod.POST)
    public ResponseEntity<Object> getCommentSellList(@PathVariable("appver") String appver, @RequestBody(required = false) CommentInfo commentInfo) {
        LOGGER_COMMENTCONTROLLER.info("get seller comment list ");
        if (commentInfo == null) {
            commentInfo = new CommentInfo();
        }
        UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        commentInfo.setLoginId(info.getLoginId());
        if (null != info.getOrganInfo() && null != info.getOrganInfo().getShopInfo()) {
            commentInfo.setOrganid(info.getOrganInfo().getId());
            commentInfo.setShopid(info.getOrganInfo().getShopInfo().getId());
        } else {
            commentInfo.setOrganid(info.getLoginId());
        }
        commentInfo.setSyscodeid(info.getSyscodeid());

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(commentService.findSellerCommentList(commentInfo));
        LOGGER_COMMENTCONTROLLER.info("get seller comment list end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 卖家评论功能--设置待回复中评价为-需要回复或不需要回复：
     * @param appver the appver
     * @return the cart
     */
    @RequestMapping(value = "/is/{appver}/order/seller/updateRestoreflag4Comment", method = RequestMethod.POST)
    public ResponseEntity<Object> updateRestoreflag4Comment(@PathVariable("appver") String appver, @RequestBody(required = false) CommentInfo commentInfo) {
       LOGGER_COMMENTCONTROLLER.info("update restoreflag in this comment table start");

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        //更新字段 restoreflag 是否必须回复标记
        try {
            int restoreFlag = commentInfo.getRestoreFlag();
            Long commentId = commentInfo.getCommentid();
            commentService.updateRestoreflag (commentId, restoreFlag);
            resultInfo.setResult(true);
        }catch (Exception e) {
            e.printStackTrace();
            resultInfo.setResult(false);
        }

        LOGGER_COMMENTCONTROLLER.info("update restoreflag in this comment table end ");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
    /**
     * 卖家删除评论
     */
    @RequestMapping(value="/is/{appver}/order/buyer/delSellerComment",method=RequestMethod.POST)
    public ResponseEntity<Object> delSellerComment(@PathVariable("appver") String appver,HttpServletRequest request) {
    	boolean resultFlag=true;
    	String message="";
    	
    	LOGGER_COMMENTCONTROLLER.info("del sellerCommnet start");
    	
    	//得到卖家回复的评论明细id和主表id
    	Long comDetailId=Long.valueOf(request.getParameter("detailId"));
    	Long commentId=Long.valueOf(request.getParameter("commentId"));
    	
    	//删除卖家评论
    	try {
			commentService.delSellerComment(comDetailId,commentId);
			
			LOGGER_COMMENTCONTROLLER.info("del sellerCommnet success");
		} catch (Exception e) {
			resultFlag=false;
			message=messageSourceUtils.getMessage("order.comment.delFail", null);
			LOGGER_COMMENTCONTROLLER.info("del sellerCommnet fail");
			e.printStackTrace();
		}
    	
    	ControllerResultInfo resultInfo=new ControllerResultInfo();
    	
    	resultInfo.setResult(resultFlag);
    	resultInfo.setData(null);
    	if(!resultFlag) {
    		resultInfo.setMsg(message);
    	}
    	
		return new ResponseEntity<Object>(resultInfo,HttpStatus.OK);
    	
    }
    
    /**
     * 置已读标记
     */
    @RequestMapping(value="/is/{appver}/order/buyer/setValueForReadFlag",method=RequestMethod.POST)
    public ResponseEntity<Object> setValueForReadFlag(@PathVariable("appver") String appver,HttpServletRequest request) {
    	boolean resultFlag=true;
    	String message="";
    	
    	LOGGER_COMMENTCONTROLLER.info("read sellerCommnet start");
    	
    	//得到评论主表id和操作对象
    	int objectFlag=Integer.valueOf(request.getParameter("objectFlag"));
    	Long commentId=Long.valueOf(request.getParameter("commentId"));
    	
    	//置已读标记
    	try {
			commentService.setValueForReadFlag(commentId,objectFlag);
			
			LOGGER_COMMENTCONTROLLER.info("read sellerCommnet success");
		} catch (Exception e) {
			resultFlag=false;
			message=messageSourceUtils.getMessage("order.comment.readFail", null);
			LOGGER_COMMENTCONTROLLER.info("read sellerCommnet fail");
			e.printStackTrace();
		}
    	
    	ControllerResultInfo resultInfo=new ControllerResultInfo();
    	
    	resultInfo.setResult(resultFlag);
    	resultInfo.setData(null);
    	if(!resultFlag) {
    		resultInfo.setMsg(message);
    	}
    	
		return new ResponseEntity<Object>(resultInfo,HttpStatus.OK);
    }
    
    /**
     * 查询卖家默认评价
     * @param appver
     * @return
     */
    @RequestMapping(value="/is/{appver}/order/seller/getDefaultAnswer",method=RequestMethod.POST)
    public ResponseEntity<Object> getDefaultAnswer(@PathVariable("appver") String appver) {
        LOGGER_COMMENTCONTROLLER.info(">>>>>> seller getDefaultAnswer start");
        CommentInfo commentInfo = new CommentInfo();
        UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        commentInfo.setLoginId(info.getLoginId());
        if (null != info.getOrganInfo() && null != info.getOrganInfo().getShopInfo()) {
            commentInfo.setOrganid(info.getOrganInfo().getId());
            commentInfo.setShopid(info.getOrganInfo().getShopInfo().getId());
        } else {
            commentInfo.setOrganid(info.getLoginId());
        }
        commentInfo.setSyscodeid(info.getSyscodeid());

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(commentService.getDefaultAnswer(commentInfo));
        LOGGER_COMMENTCONTROLLER.info(" <<<<<< seller getDefaultAnswer end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
    /**
     * 保存卖家默认回复信息
     */
    @RequestMapping(value="/is/{appver}/order/seller/saveDefaultMsg",method=RequestMethod.POST)
    public ResponseEntity<Object> saveDefaultMsg(@PathVariable("appver") String appver,@RequestBody(required = false) CommentDefaultAnswerInfo cdaInfo) {
    	boolean resultFlag=true;
    	String message="";
    	
    	LOGGER_COMMENTCONTROLLER.info("save defaultMsg sellerCommnet start");
    	
    	if (SecurityUtils.getSubject().isAuthenticated()) {
            UserInfo info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            cdaInfo.setSellerSyscodeId(info.getSyscodeid());
        } else {
            LOGGER_COMMENTCONTROLLER.info("未登录");
        }
    	
    	//保存默认信息
    	try {
    		commentDefaultAnswerService.saveDefaultMsg(cdaInfo);
			
			LOGGER_COMMENTCONTROLLER.info("save defaultMsg sellerCommnet success");
		} catch (Exception e) {
			resultFlag=false;
			message=messageSourceUtils.getMessage("order.comment.saveDefaultMsgFail", null);
			LOGGER_COMMENTCONTROLLER.info("save defaultMsg sellerCommnet fail");
			e.printStackTrace();
		}
    	
    	ControllerResultInfo resultInfo=new ControllerResultInfo();
    	
    	resultInfo.setResult(resultFlag);
    	resultInfo.setData(null);
    	if(!resultFlag) {
    		resultInfo.setMsg(message);
    	}
    	
		return new ResponseEntity<Object>(resultInfo,HttpStatus.OK);
    	
    } 
    
}
